
package com.bigfreakindeals.Activities;

import java.util.ArrayList;
import java.util.Currency;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.TabHost.TabContentFactory;
import com.bigfreakindeals.R;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Controller.GPSController;
import com.bigfreakindeals.Models.UserProfile;
import com.paypal.android.MEP.PayPalActivity;

public class MainTabActivity extends TabActivity {

	TabHost mTabHost;
	 

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 System.out.println("mainTab Step 1");
		
	 setContentView(R.layout.main);
	 
	 mTabHost = (TabHost) findViewById(android.R.id.tabhost);

	 View tabview = createTabView(mTabHost.getContext(), R.layout.tab_activedeal);
	 Intent intent = new Intent().setClass(this, ActiveDealsActivityGroup.class);
	 TabSpec setContent = mTabHost.newTabSpec("Active Deals Groups").setIndicator(tabview).setContent(intent);
	 mTabHost.addTab(setContent);
	
	 tabview = createTabView(mTabHost.getContext(), R.layout.tab_mycoupons);
	 intent = new Intent().setClass(this, MyCouponActivity.class);
	 setContent = mTabHost.newTabSpec("My Coupons").setIndicator(tabview).setContent(intent);
	 mTabHost.addTab(setContent);
	 
	 tabview = createTabView(mTabHost.getContext(), R.layout.tab_settings);
	 intent = new Intent().setClass(this, SettingsActivity.class);
	 setContent = mTabHost.newTabSpec("Settings").setIndicator(tabview).setContent(intent);
	 mTabHost.addTab(setContent);
	 
		this.registerReceiver(broadcast, new IntentFilter(Constant.EVENTS_UPDATECURRENTTAB));
		
	
	 
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(broadcast);

	}
	
	  @Override
	   public void onResume(){
		  // Start the root activity withing the group and get its view  
		  if  (ApplicationEx.getInstance().myProfile == null){//*start of a new session*//
			  Intent splash = new Intent(this,SplashScreenActivity.class);
		  	  startActivity(splash);
		  			
		}else if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool()==null){ //*start of a new session*//
			 Intent splash = new Intent(this,SplashScreenActivity.class);
			 startActivity(splash);
	
		}else {
			  mTabHost.setCurrentTab(ApplicationEx.getInstance().currentTab);
		}
	  
	  //check db
	   super.onResume();
		 
	   	    
	  }
		public BroadcastReceiver broadcast = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				
				if (intent.getAction().equals(Constant.EVENTS_UPDATECURRENTTAB)) {
					mTabHost.setCurrentTab(ApplicationEx.getInstance().currentTab);
				}
					
			}
		};
		
		private static View createTabView(final Context context, final int layout) {
		    View view = LayoutInflater.from(context).inflate(layout, null);
		 return view;
		
}
		 public void setConfirmAlert(final int keyCode, final KeyEvent event){
			   
			  	AlertDialog.Builder builder = new AlertDialog.Builder(this);
					builder.setMessage("Do you want to exit BigFreakinDeals app")
					       .setCancelable(false)
					       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					        	   dialog.cancel(); 
					        	   dialog.dismiss();
					        	   	ApplicationEx.getInstance().isConfirm = true;
					        	   if (event == null)
					        		onBackPressed();
					        	   else
					        		   onKeyDown(keyCode, event);
					        	   
					           }
					       })
					       .setNegativeButton("No", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					                ApplicationEx.getInstance().isConfirm = false;
					           }
					       });
					AlertDialog alert = builder.create();
					alert.show();
					
				}
		
		/* @Override    
		   public void onBackPressed() {
			 	
		           if(ApplicationEx.getInstance().history!=null && ApplicationEx.getInstance().history.size() > 1) {  
		        	   ApplicationEx.getInstance().history.remove(ApplicationEx.getInstance().history.size()-1);  
		               setContentView(ApplicationEx.getInstance().history.get(ApplicationEx.getInstance().history.size()-1)); 
		           }else {
		        	   
		           	if (!ApplicationEx.getInstance().isConfirm){
		           		if (ApplicationEx.getInstance().currentTab!=0){
			   				ApplicationEx.getInstance().currentTab = 0;
			   				return ;
			   			}
		           		setConfirmAlert(KeyEvent.KEYCODE_BACK,null);  	
		           	}else {
		           	//   moveTaskToBack(true);
		        	 Intent i = new Intent();
		        	   i.setAction(Intent.ACTION_MAIN);
		        	   i.addCategory(Intent.CATEGORY_HOME);
		        	   this.startActivity(i);
		        	   ApplicationEx.getInstance().myProfile = null;
		           	}
		           }
		       
		       return;
		   }
		 @Override  
		public boolean onKeyDown(int keyCode, KeyEvent event) {
		   		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
		   			if (ApplicationEx.getInstance().currentTab!=0){
		   				ApplicationEx.getInstance().currentTab = 0;
		   				return true;
		   			}
		   			
		           if(ApplicationEx.getInstance().history!=null && ApplicationEx.getInstance().history.size() > 1) {  
		        	   ApplicationEx.getInstance().history.remove(ApplicationEx.getInstance().history.size()-1);  
		               setContentView(ApplicationEx.getInstance().history.get(ApplicationEx.getInstance().history.size()-1)); 
		               return true;
		           }else {
		        	   if (!ApplicationEx.getInstance().isConfirm){
		              		if (ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getSchoolSlug().
		              				compareTo(ApplicationEx.getInstance().myProfile.getHomeUniversity().getSchoolSlug()) == 0) //true
		              			{
		              			if (ApplicationEx.getInstance().currentTab!=0){
		              				ApplicationEx.getInstance().currentTab = 0;
		              				return true;
		              			}
		              			setConfirmAlert(KeyEvent.KEYCODE_BACK,null);  	
		                       
		              			}else {
		              				
		              				ApplicationEx.getInstance().myProfile.setlastSelectedSchool(ApplicationEx.getInstance().myProfile.getHomeUniversity());
		              				// sethistory
		              				ApplicationEx.getInstance().history =new ArrayList<View>();  
		              				
		              				view = getLocalActivityManager().startActivity("ActiveDeals", new  
		                                Intent(this,ActiveDealsActivity.class)  
		                               .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
		                                .getDecorView();  

		   						// Replace the view of this ActivityGroup  
		   						replaceView(view);
		   						
		              				
		              			}
		              	
		              	}else {
		              	 //  moveTaskToBack(true);
		           	  Intent i = new Intent();
		           	   i.setAction(Intent.ACTION_MAIN);
		           	   i.addCategory(Intent.CATEGORY_HOME);
		           	   this.startActivity(i);
		           	   ApplicationEx.getInstance().isConfirm = false;
		           	   ApplicationEx.getInstance().myProfile = null;
		           	   ApplicationEx.getInstance().schoolListMap = null;
		              	}
		              }
		       return super.onKeyDown(keyCode, event);
		   }*/
		 
		 @Override
		 public void onConfigurationChanged(Configuration newConfig) {
		 super.onConfigurationChanged(newConfig);
		 }
	
		     
}
